package com.wang.example.netty.handler.client;

import com.alibaba.fastjson.JSON;
import com.wang.basic.netty.handler.Message;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.util.CharsetUtil;

/**
 * 该处理器将对象转为字节 发送到网络上
 */
public class EchoClientOutHandler1 extends ChannelOutboundHandlerAdapter {
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        System.out.println("EchoClientOutHandler1 处理："+msg);
        Message message = (Message)msg;
        String messageJson = JSON.toJSONString(message);
        ByteBuf byteBuf = Unpooled.copiedBuffer(messageJson, CharsetUtil.UTF_8);
        ctx.write(byteBuf, promise);
    }
}
